package com.pjj.transactionservice.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pjj.transactionservice.Entity.Transaction;
import com.pjj.transactionservice.mapper.TransactionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author 76004
 * @apiNote
 * @date 2023/2/15 11:50
 */
@Service
public class TransactionService {
    @Autowired
    private TransactionMapper transactionMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public Object selectById(long id){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","查询成功");
        map.put("data",transactionMapper.selectById(id));
        return map;
    }

    public Object insertTransaction(Transaction transaction){
        Date date = new Date();//获得当前时间
        Timestamp dateFormat = new Timestamp(date.getTime());
        transaction.setTime(dateFormat);
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","添加成功");
        transaction.setStatus(new Random().nextInt()%8+1);
        transactionMapper.insert(transaction);
        redisTemplate.opsForValue().increment("transaction");
        return map;
    }

    public Object deleteById(long id){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","删除成功");
        transactionMapper.deleteById(id);
        redisTemplate.opsForValue().decrement("transaction");
        return map;
    }

    public Object updateTransaction(Transaction transaction){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","更新成功");
        transactionMapper.updateById(transaction);
        return map;
    }

    public Object selectPage(int pageNum,int pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","查询成功");
        IPage<Transaction> page = transactionMapper.selectPage(new Page(pageNum,pageSize),new QueryWrapper<Transaction>());
        map.put("data",page.getRecords());
        System.out.println(map.toString());
        return map;
    }

    public Object selectCount(){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","查询成功");
        map.put("data",transactionMapper.selectCount(Wrappers.emptyWrapper()));
        return map;
    }

    public Object selectByStatus(int status,int pageNum,int pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","查询成功");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status",status);
        map.put("data",transactionMapper.selectPage(new Page<Transaction>(pageNum,pageSize),queryWrapper).getRecords());
        return map;
    }

    public Object selectCountByStatus(int status){
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        map.put("msg","查询成功");
        QueryWrapper queryWrapper = new QueryWrapper();
        map.put("data",transactionMapper.selectCount(queryWrapper.eq(true,"status",status)));
        return map;
    }

}
